<?php

require_once('admincontroller.php');

class Config extends AdminController
{
	function Config()
	{
		parent::AdminController();
		$this->load->model('User');
		$this->main_navigation = 'config';
		$this->data = array();
		$this->themes = array(
			'transdmin' => 'Transdmin',
			'flick' => 'Flick'
		);
	}

	function index()
	{
		$this->sub_navigation = 'settings';
		$this->settings();
	}

	function settings()
	{
		$data['settings'] = $this->ApplicationConfig->all('hidden != 1');
		$data['advanced_view'] = 0;
		$this->display('settings', $data);
	}

	function advanced_settings()
	{
		$this->sub_navigation = 'settings';
		$data['settings'] = $this->ApplicationConfig->all();
		$data['advanced_view'] = 1;
		$this->display('settings', $data);
	}

	function setting_save($id)
	{
		$value = clean_str($_POST['value']);
		$setting = $this->ApplicationConfig->get_by_id($id);
		if ($this->ApplicationConfig->update($setting, $value) && $this->ApplicationConfig->save($setting)) {
			return;
		}
		$this->output->set_status_header(400);
	}

	// administrators ----------------------------------------------------------

	function administrators()
	{
		$this->sub_navigation = 'admins';
		$data['users'] = $this->User->all('s.code = "admin"');
		$this->display('administrators', $data);
	}

	function admin_new()
	{
		$this->sub_navigation = 'admins';
		$data['user'] = new User();
		$this->display('admin_new', $data);
	}

	function admin_create()
	{
		$user = new User();
		$user->created_at = $user->updated_at = date_db_format();
		$this->User->set_status_from_code($user, 'admin', 0, true);

		$errors = $this->User->update($user, $_POST);

		if (count($errors) == 0 && $this->User->save($user)) {
			$data['success_notice'] = 'Administrateur créé !';
			$data['users'] = $this->User->all('s.code = "admin"');
			$this->display('administrators', $data);
		}
		else {
			$data['error_notice'] = $errors;
			$data['user'] = $user;
			$this->display('admin_new', $data);
		}
	}

	// profile -----------------------------------------------------------------

	function my_profile()
	{
		$this->data['user'] = $this->User->get_by_id($this->session->userdata('user_id'));
		$this->data['themes'] = $this->themes;
		$this->display('admin_edit', $this->data);
	}

	function profile_update()
	{
		$user = $this->User->plain_get($this->session->userdata('user_id'));
		$user->updated_at = date_db_format();
		$errors = $this->User->update($user, $_POST);
		if (count($errors) == 0 && $this->User->save($user)) {
			$sess = array(
				'email' => $user->email,
				'name' => $user->name,
				'firstname' => $user->firstname
			);
			$this->session->set_userdata($sess);
			$data['success_notice'] = 'Profil mis à jour !';
			$data['user'] = $user;
		}
		else {
			$data['error_notice'] = $errors;
			$data['user'] = $user;
		}
		$data['themes'] = $this->themes;
		$this->display('admin_edit', $data);
	}

	function profile_change_password()
	{
		$user = $this->User->plain_get($this->session->userdata('user_id'));
		$user->updated_at = date_db_format();
		$errors = $this->User->update_password($user, $_POST);
		if (count($errors) == 0 && $this->User->save($user)) {
			$data['success_notice'] = 'Mot de passe mis à jour !';
		}
		else {
			$data['error_notice'] = $errors;
		}
		$data['user'] = $user;
		$data['themes'] = $this->themes;
		$this->display('admin_edit', $data);
	}

	function profile_update_settings()
	{
		$this->session->set_userdata('theme', $_POST['theme']);
		$this->data['success_notice'] = 'Préférences mises à jour !';
		$this->my_profile();
	}

	function server_tools()
	{
		$this->sub_navigation = 'server';
		$this->display('server_tools');
	}
}
